#!/usr/bin/perl/

use strict;
use warnings;

my $chain = 'B';

# operates on this directory

my @files = <*.pdb>;
my %scores;
my $log = "processed_dir.txt";

my %aa_3_to_1 = ("ala", 'A',
"cys", 'C',
"asp", 'D',
"glu", 'E',
"phe", 'F',
"gly", 'G',
"his", 'H',
"ile", 'I',
"lys", 'K',
"leu", 'L',
"met", 'M',
"asn", 'N',
"pro", 'P',
"gln", 'Q',
"arg", 'R',
"ser", 'S',
"thr", 'T',
"val", 'V',
"trp", 'W',
"tyr", 'Y',
);

open LOG, "+>", $log or die ("Could not open $log!\n$!\n");
print "Opened $log\n";
foreach my $file (@files) {
	#print $file . "\n";
	open FILE, $file;
	while (<FILE>) {
		if (/^pose/) {
			my @lineobjs = split;
			$scores{$file} = $lineobjs[20];
		}
	}
	
	close (FILE);
}

# scores now contains each file's score, indexed by filename
print "step one success\n";

# filters for the top 5% of files by score
my $imax = scalar(keys %scores) / 20;

my $i = 0;
my @bestfiles;
foreach my $file (sort {$scores{$a} <=> $scores{$b}} keys %scores) {
	if ($i < $imax) {
		print LOG $scores{$file} . "\t$file\n";
	}
	$i++;
}
close LOG;

